<?php
/**
 * @name /www/unit-tests.php
 * @author Michael Foss <michael@mikefoss.com>
 * @version 2011.03.26
 *
 * Unit test for Orphanage class.
 */

// Sets up configuration settings and includes libraries.
require_once('_ini.php');
// Libraries
ReqLib('/lib/private/Orphanage.php');

function DefineHeader() {
?><style type="text/css">
  code {
    font: bold 10pt 'Courier New';
    color: #666;
    background-color: #CCC;
    border: 3px solid #666;
    padding: 4px 6px;
    margin: 10px 20px;
    display: block;
  }
  form, ul, ol {
    margin: 0px 20px;
  }
  a#Back {
    float: right;
    margin: 10px;
    padding: 4px 6px;
    border: 1px solid #666;
    background-color: #CCC;
  }
</style><?
}

/**
 * Defines page content.
 *
 */
function DefineContent() {
/*
    "GET":
    Orphanage[] GetOrphanages(int offset, int maxResults)
    bool Exists(int orphanageId)
    string ToString()

    "SCUD":
    bool Load(int orphanageId)
    bool Create(name)
    bool Save()
    static bool Delete(int orphanageId)

    Custom:
    static int GetOrphanageIdByOrphanageName(string name)
*/
  $actions = array(
    'Delete(id)',
    'Delete All',
    'Create',
    'View Details',
    'List(offset,max)',
    'GetIdByName(name)',
    'Save'
  );
  // Page content goes here.
  ?>
<h1><a href="Orphanage.php">Orphanage - Unit Test</a></h1>
<a id="Back" href="index.php">Back to Unit Testing Home</a>
<form method="GET">
  <label for="Param1">Param 1: </label><input type="text" value="<?=htmlspecialchars($_GET['Param1']) ?>" name="Param1" /><br />
  <label for="Param2">Param 2: </label><input type="text" value="<?=htmlspecialchars($_GET['Param2']) ?>" name="Param2" /><br />
<?
  foreach ($actions as $action)
    println('<input type="submit" name="Action" value="' . $action . '" /><br />');
?><br />
<hr />
<?
  DoAction();
?></form>
<?
}

function DoAction() {
?><p>
<?
  switch ($_GET['Action']) {
    case 'Delete(id)':
      println(Orphanage::Delete($_GET['Param1']) ? 'Deleted' : 'Not deleted');
      break;

    case 'Delete All':
      $os = Orphanage::GetOrphanages();
      foreach ($os as $o)
        Orphanage::Delete($o->OrphanageId);
      println('Data cleared.');
      break;

    case 'Create':
      $o = new Orphanage();
      $ids = array(
        '',
        '1',
        '2',
        rand(1, 5)
      );
      foreach ($ids as $id)
        println("Creating id '$id': " . ($o->Create($id) ? 'Created' : 'Not created') . '<br />');
      break;

    case 'List(offset,max)':
      println('<ol>');
      foreach(Orphanage::GetOrphanages($_GET['Param1'], $_GET['Param2']) as $o)
        println("<li>{$o->OrphanageId}: {$o->Name}</li>");
      println('</ol>');
      break;

    case 'View Details':
      println('<textarea style="width: 600px; height: 400px;">' . print_r(Orphanage::GetOrphanages(0, 1), true) . '</textarea>');
      break;

    case 'GetIdByName(name)':
      println("ByName('" . htmlspecialchars($_GET['Param1']) . "') = '" . Orphanage::GetOrphanageIdByOrphanageName($_GET['Param1']) . "'");
      break;

    case 'Save':
      $o = Orphanage::GetOrphanages(0, 1);
      $o = $o[0];
      $o->Name = rand(1, 100);
      $o->ContactFirstName = rand(1, 100);
      $o->ContactLastName = rand(1, 100);
      $o->ContactPhoneNumber = rand(1, 100);
      $o->Street1 = rand(1, 100);
      $o->Street2 = rand(1, 100);
      $o->Street3 = rand(1, 100);
      $o->City = rand(1, 100);
      $o->State = rand(1, 100);
      $o->Country = rand(1, 100);
      $o->PostalCode = rand(1, 100);
      $o->Notes = rand(1, 100);
      println($o->Save() ? 'Saved' : 'Not saved');
      println('<br /><textarea style="width: 600px; height: 400px;">' . print_r($o->ToString("\r\n"), true) . '</textarea>');
      break;

    default:
      break;
  }
?></p>
<?
}

// Launches the page; without this, nothing happens.
require_once('_loader.php');